package com.wuhongyu.rabbitmq.producer.rabbitmq;

import com.rabbitmq.client.Channel;
import com.wuhongyu.api.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class DlxConsumer {


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(RabbitmqConstant.DLX_QUEUE_NAME),
                    exchange = @Exchange(value = RabbitmqConstant.DLX_EXCHANGE,type = "topic"),
                    key = RabbitmqConstant.DLX_ROUTING_KEY,
                    ignoreDeclarationExceptions = "true"
            )
    })
    @RabbitHandler
    public void receive(@Payload Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long del , @Headers Map<String,Object> map) throws IOException {
        log.info("死信队列收到消息： {}", message);
        String type = message.getType();
        if ("error".equals(type)){
            channel.basicAck(del, false);
            log.info("死信队列处理异常完成！！！！");
        }

    }
}
